package com.project.bohang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bohang.common.exception.BusinessException;
import com.project.bohang.common.model.Response;
import com.project.bohang.entity.EsGoods;
import com.project.bohang.entity.House;
import com.project.bohang.entity.SendGoodsRecord;
import com.project.bohang.entity.UserSendGoods;
import com.project.bohang.mapper.EsGoodsMapper;
import com.project.bohang.mapper.HouseMapper;
import com.project.bohang.mapper.SendGoodsRecordMapper;
import com.project.bohang.mapper.UserSendGoodsMapper;
import com.project.bohang.pojo.dto.EsGoodsDto;
import com.project.bohang.pojo.dto.SendEsGoodsDto;
import com.project.bohang.pojo.vo.HouseVo;
import com.project.bohang.service.EsGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.management.Query;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bohang
 * @since 2022-03-26
 */
@Service
public class EsGoodsServiceImpl extends ServiceImpl<EsGoodsMapper, EsGoods> implements EsGoodsService {

    @Resource
    private EsGoodsMapper esGoodsMapper;

    @Resource
    private SendGoodsRecordMapper sendGoodsRecordMapper;

    @Resource
    private HouseMapper houseMapper;

    @Resource
    private UserSendGoodsMapper userSendGoodsMapper;

    @Override
    public IPage<EsGoods> selectList(EsGoodsDto dto) {
        IPage<EsGoods> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        List<EsGoods> list = esGoodsMapper.goodsList(page, dto);
        page.setRecords(list);
        return page;
    }

    @Override
    @Transactional
    public Response send(SendEsGoodsDto req) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //插入发放给用户的信息
        for (Long id :  req.getHouseIds()) {
            UserSendGoods userSendGoods = new UserSendGoods();
            userSendGoods.setContext(req.getContext());
            userSendGoods.setEsNums(req.getNums());
            userSendGoods.setSendTime(LocalDateTime.parse(req.getSendTime(), formatter));
            userSendGoods.setEsId(req.getId());
            userSendGoods.setHouseId(id);
            userSendGoodsMapper.insert(userSendGoods);
        }
        SendGoodsRecord sendGoodsRecord = new SendGoodsRecord();
        sendGoodsRecord.setSendTime(LocalDateTime.parse(req.getSendTime(), formatter));
        sendGoodsRecord.setEsNums(req.getNums());
        sendGoodsRecord.setSendType(req.getSendType());
        String[] houseNamesArr = req.getHouseNames().toArray(new String[0]);
        StringBuilder houseNames = new StringBuilder();
        for (String hn : houseNamesArr) {
            houseNames.append(hn).append(" ");
        }
        sendGoodsRecord.setHouseNames(houseNames.toString());
        sendGoodsRecord.setEsId(req.getId());
        sendGoodsRecord.setContext(req.getContext());
        //插入物品投放表
        sendGoodsRecordMapper.insert(sendGoodsRecord);
        //发了n户
        Integer num = req.getHouseIds().size();
        //该物资信息
        EsGoods esGoods = esGoodsMapper.selectById(req.getId());
        if (esGoods.getNums() < num * req.getNums()) {
            throw new BusinessException("60002", "库存不足了");
        }
        //扣除库存
        esGoodsMapper.reduceNums(req.getId(), num * req.getNums());

        return Response.success();
    }
}
